package com.qudian.java.core.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * json操作类
 * @author wangyunfeng
 *
 */
public class ParserJsonArray {
	/**
	 * json字符串转换成bean
	 * @param str
	 * @param clazz
	 * @return
	 */
    public static <T> List<T> parserJsonArray(String str, Class<T> clazz) {
        List<T> list;
        if (StringUtils.isBlank(str)) {
            return Collections.emptyList();
        }
        list = JSONArray.parseArray(str, clazz);

        if (ObjectUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list;
    }
    
    /**
	 * json字符串转换成bean
	 * @param obj obj
	 * @return String
	 */
    public static String parserBean(Object obj) {
    	if (ObjectUtil.allNull(obj)){
    		return null;
    	}
        return JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect);
    }
}
